#include "KCPClientTest.h"
#include "KCP/KCPClient.h"
#include <QDebug>
#include <QDateTime>
KCPClient* client;
int64_t rtt;
KCPClientTest::KCPClientTest(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::KCPClientTestClass())
{
    ui->setupUi(this);
	timer = new QTimer(this);
	connect(timer, &QTimer::timeout, this, &KCPClientTest::Tick);
	timer->start(3);
    client = new KCPClient();
    client->RegisterConnectCompletedCallback(&KCPClientTest::OnConnectCompleted, this);
    client->RegisterDisconnectedCallback(&KCPClientTest::OnDisconnected, this);
    client->RegisterReceivedCallback(&KCPClientTest::OnReceived, this);
  
}

KCPClientTest::~KCPClientTest()
{
    delete ui;
}

void KCPClientTest::Tick()
{
    client->Update();
}

void KCPClientTest::on_Connect_clicked()
{
    client->AsyncConnect("127.0.0.1", 5555);
}

void KCPClientTest::on_Disconnect_clicked()
{
    client->Disconnect();
}

void KCPClientTest::on_Send_clicked()
{
	const char* msg = "hello";
	client->Send((char*)msg, strlen(msg));
    rtt = QDateTime::currentMSecsSinceEpoch();
}

void KCPClientTest::OnConnectCompleted(int code, std::string error)
{

}

void KCPClientTest::OnDisconnected()
{

}

void KCPClientTest::OnReceived(char* data, int len)
{
	rtt = QDateTime::currentMSecsSinceEpoch() - rtt;
	qDebug() << "rtt : " << rtt;
	QByteArray ba(data, len);
	qDebug() << ba;
    //client->Send(data, len);

}
